<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
class BakerysViewItems extends JViewLegacy
{
	protected $items;
	protected $pagination;
	protected $state;
	
	public function display($tpl = null) 
	{
		$this->state = $this->get('State');
		$this->items = $this->get('Items');
		$this->pagination	= $this->get('Pagination');
		BakerysHelper::addSubmenu('items');
		if (count($errors = $this->get('Errors'))) 
		{
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		
		$this->addToolBar();
		$this->sidebar = JHtmlSidebar::render();
		parent::display($tpl);
	
	}

	protected function addToolBar() 
	{
		require_once JPATH_COMPONENT.'/helpers/bakerys.php';
		$state	= $this->get('State');
	
		$canDo	= BakerysHelper::getActions($state->get('filter.category_id'));
		JToolbarHelper::title(JText::_('COM_BAKERYS_ITEMS'));
		if ($canDo->get('core.create'))
		{
			JToolbarHelper::addNew('item.add');
		}
		if ($canDo->get('core.edit'))
		{
			JToolbarHelper::editList('item.edit');
		}
		if ($canDo->get('core.edit.state')) {

			JToolbarHelper::publish('items.publish', 'JTOOLBAR_PUBLISH', true);
			JToolbarHelper::unpublish('items.unpublish', 'JTOOLBAR_UNPUBLISH', true);

			JToolbarHelper::archiveList('items.archive');
		}
		if ($state->get('filter.state') == -2 && $canDo->get('core.delete'))
		{
			JToolbarHelper::deleteList('', 'items.delete', 'JTOOLBAR_EMPTY_TRASH');
		} elseif ($canDo->get('core.edit.state'))
		{
			JToolbarHelper::trash('items.trash');
		}
		
		if ($canDo->get('core.admin'))
		{
			JToolbarHelper::preferences('com_bakerys');
		}
		
		
		
		JHtmlSidebar::addFilter(
			JText::_('JOPTION_SELECT_PUBLISHED'),
			'filter_state',
			JHtml::_('select.options', JHtml::_('jgrid.publishedOptions'), 'value', 'text', $this->state->get('filter.state'), true)
		);

	}
	
	protected function getSortFields()
	{
		return array(
			'a.ordering' => JText::_('JGRID_HEADING_ORDERING'),
			'a.state' => JText::_('JSTATUS'),
			'a.title' => JText::_('COM_BAKERYS_ITEMS_TITLE'),
			'a.id' => JText::_('JGRID_HEADING_ID')
		);
	}	
}
